Download
FAQ
History
HomeHomeNext API
Search
Feedback
Divider

Using JAXR Clients in J2EE Applications

You can create J2EE applications that use JAXR clients to access registries. This section explains how to write, compile, package, deploy, and run a J2EE application that uses JAXR to publish an organization to a registry and then query the registry for that organization. The application in this section uses two components, an application client and a stateless session bean.

The section covers the following topics:

You will find the source files for this section in the directory <INSTALL>/j2eetutorial14/examples/jaxr/clientsession. Path names in this section are relative to this directory.

Coding the Application Client: MyAppClient.java

The application client class, src/MyAppClient.java, obtains a handle to the PubQuery enterprise bean's remote home interface, using the JNDI API naming context java:comp/env. The program then creates an instance of the bean and calls the bean's two business methods, executePublish and executeQuery.

Before you compile the application, edit the PubQueryBeanExamples.properties file the same way you edited the JAXRExamples.properties file to run the simple examples.

  1. Leave the queryManagerURL and lifeCycleManagerURL entries as they are if you are using the Java WSDP Registry Server on your local system. To use another registry, comment out the property that specifies the Registry Server and remove the comment from the other registry.
  2. If you are using a public registry, change the values for the http.proxyHost and https.proxyHost entries so that they specify the the system on your network through which you access the Internet.

Coding the PubQuery Session Bean

The PubQuery bean is a stateless session bean with one create method and two business methods. The bean uses remote interfaces rather than local interfaces because it is accessed from outside the EJB container.

The remote home interface source file is src/PubQueryHome.java.

The remote interface, src/PubQueryRemote.java, declares two business methods, executePublish and executeQuery.The bean class, src/PubQueryBean.java, implements the executePublish and executeQuery methods and their helper methods getName, getDescription, and getKey. These methods are very similar to the methods of the same name in the simple examples JAXRQuery.java and JAXRPublish.java. The executePublish method uses information in the file PubQueryBeanExample.properties to create an organization named The Coffee Enterprise Bean Break. The executeQuery method uses the string "Coff", specified in the application client code, to locate this organization.

The bean class also implements the required methods ejbCreate, setSessionContext, ejbRemove, ejbActivate, and ejbPassivate.

The ejbCreate method of the bean class allocates resources--in this case, by looking up the ConnectionFactory and creating the Connection.

The ejbRemove method must deallocate the resources that were allocated by the ejbCreate method. In this case, the ejbRemove method closes the Connection.

Compiling the Source Files

To compile the application source files, use the following command:

asant compile 

The compile target places the properties file and the class files in the build directory.

Starting the J2EE Application Server

To run this example, you need to start the J2EE Application Server.

Creating JAXR Resources

In order to use JAXR in a J2EE application that uses the J2EE 1.4 Application Server, you need to access the JAXR resource adapter (see Implementing a JAXR Client. The following commands (each must be all on one line) create the resources needed to access the resource adapter:

asadmin create-connector-connection-pool --user username 
--password password --raname jaxr-ra --connectiondefinition
javax.xml.registry.ConnectionFactory jaxr-pool

asadmin create-connector-resource --user username --password
password --poolname jaxr-pool eis/JAXR 

The build.xml file for this example has targets named create-pool and create-resource that automate this task. The create-resource task depends on create-pool.

After you start the server, perform the following steps:

  1. Specify the following asant target:
  2. asant create-resource

  3. Reconfigure the server:
  4. asant reconfig_common

If the output from this command says that an instance restart is required, stop and restart the server.

Creating and Packaging the Application

Creating and packaging this application involve several steps:

Starting the J2EE Deploytool and Creating the Application

  1. Start the deploytool:
  2. deploytool

  3. Choose FileRight ArrowNewRight ArrowApplication EAR.
  4. Click Browse next to the Application File Name field and use the file chooser to locate the directory clientsession.
  5. In the File Name field, type ClientSessionApp.
  6. Click New Application.
  7. Click OK.

Packaging the Application Client

  1. Choose FileRight ArrowNewRight ArrowApplication Client JAR to start the Application Client Wizard, then click Next.
  2. In the JAR File Contents screen:
    1. Make sure that Create New AppClient Module in Application is selected and that the application is ClientSessionApp.
    2. In the AppClient Display Name field, type MyAppClient.
    3. Click the Edit button next to the Contents text area.
    4. In the dialog box, locate the clientsession/build directory. Select MyAppClient.class from the Available Files tree area and click Add, then OK.
  3. In the General screen, select MyAppClient in the Main Class combo box.
  4. In the EJB References screen, click Add. In the dialog box:
    1. Type ejb/remote/PubQuery in the Coded Name field.
    2. Choose Session from the EJB Type menu.
    3. Type PubQueryHome in the Home Interface field.
    4. Type PubQueryRemote in the Local/Remote Interface field.
    5. Leave the Enterprise Bean Name field empty.
    6. When you return to the EJB References screen, choose the JNDI Name radio button and type PubQuery. The session bean uses remote interfaces, so the client accesses the bean through the JNDI name rather than the bean name.

  5. Click Finish.

Packaging the Session Bean

  1. Choose FileRight ArrowNewRight ArrowEnterprise JavaBean JAR to start the Enterprise Bean Wizard, then click Next.
  2. In the EJB JAR screen:
    1. Select Create New JAR Module in Application and make sure that the application is ClientSessionApp.
    2. In the JAR Display Name field, type PubQueryJAR.
    3. Click the Edit button next to the Contents text area.
    4. In the dialog box, locate the clientsession/build directory. Select PubQueryBean.class, PubQueryHome.class, PubQueryRemote.class, and PubQueryBeanExample.properties from the Available Files tree area and click Add, then OK.
  3. In the General screen:
    1. Choose the Session radio button, then the Stateless radio button.
    2. From the Enterprise Bean Class menu, choose PubQueryBean.
    3. In the Enterprise Bean Display Name field, type PubQuery.
    4. In the Remote Interfaces area, choose PubQueryHome from the Remote Home Interface menu and PubQueryRemote from the Remote Interface menu.
  4. In the Configuration Options screen, check the Resource References and Transaction Management boxes.
  5. In the Transaction Management screen, choose Container-Managed.
  6. In the Resource References screen:
    1. Click Add.
    2. In the Coded Name field, type eis/JAXR.
    3. From the Type menu, choose javax.xml.registry.ConnectionFactory.
    4. In the Deployment Settings area, type eis/JAXR in the JNDI name field and j2ee in both the User Name and Password fields. (These are the user name and password specified in the file <J2EE_HOME>/domains/domain1/server/config/sun-acc.xml, which the application client container uses.)

Checking the JNDI Names

Verify that the JNDI names for the application components are correct. They should appear as shown in Table 10-3 and Table 10-4.

Table 10-3 Application Pane
Component Type
Component
JNDI Name
EJB
PubQuery
PubQuery

Table 10-4 References Pane
Ref. Type
Referenced By
Reference Name
JNDI Name
EJB Ref
MyAppClient
ejb/remote/PubQuery
PubQuery
Resource
PubQuery
eis/JAXR
eis/JAXR

Deploying the Application

  1. Choose FileRight ArrowSave to save the application.
  2. Choose ToolsRight ArrowDeploy.
  3. In the dialog box, type your administrative user name and password (if they are not already filled in) and click OK.
  4. In the dialog box that asks if you wish to save the application, click Yes.
  5. In the Distribute Module dialog box, click Close when the process completes.

Saving the Client JAR and Running the Application

  1. If you are using the Java WSDP Registry Server, start it by starting Tomcat.
  2. Windows systems: Choose StartRight ArrowProgramsRight ArrowJava(TM) Web Services Developer PackRight ArrowStart Tomcat.

    UNIX systems:

    <JWSDP_HOME>/bin/startup.sh

    Starting Tomcat takes some time.

  3. In the deploytool, select the server node.
  4. Select ClientSessionApp from the list of deployed objects, then click Client Jar.
  5. Choose Browse to navigate to the directory from which you will run the client; we suggest the examples/jaxr/clientsession directory. When you reach the directory, click Select, then click OK. Click OK in the information dialog. You will find a file named ClientSessionAppClient.jar in the specified directory.
  6. To run the client, use the following command:
  7. appclient -client ClientSessionAppClient.jar

The program output in the terminal window looks like this:

Looking up EJB reference
Looked up home
Narrowed home
Got the EJB
See server log for bean output 

In the server log, you will find the output from the executePublish and executeQuery methods.

Undeploying and Removing the Application

To undeploy and remove the application, perform the following steps:

  1. Select the deployed application in the server pane and click Undeploy.
  2. Select the application in the left-hand pane and choose FileRight ArrowClose.
  3. Remove the build directory created by the compile target:
  4. asant clean

  5. If you wish, stop the server:
  6. asadmin stop-domain

If you wish, you can manually delete the EAR and client JAR files.

Divider
Download
FAQ
History
HomeHomeNext API
Search
Feedback
Divider

All of the material in The J2EE 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.